package com.pp.buvaka.table;

import java.util.ArrayList;
import java.util.Iterator;


public class Table implements Iterable<Row> {


    private  ArrayList<Row> rows = new ArrayList<Row>(); // Table - is a list of rows

    public void addRow(Row row){
        rows.add(row);
    }

    public Field getField (String columnIndex, int rowIndex){
        return rows.get(rowIndex-1).get(columnIndex);
    }
    public Field getField (String address){
        String columnIndex = address.substring(0,1).toUpperCase();

        try {
        int rowIndex = Integer.parseInt(address.substring(1));
            return getField(columnIndex, rowIndex);
        } catch (IndexOutOfBoundsException e) {
            return new Field("0.0");
        } catch (NumberFormatException e){
            return new Field("NaN");
        }
    }

    public int getRowsCount(){
        return rows.size();
    }


    public String getColumnsMaxAlpha(){
        return Alphabet.getByNum(rows.get(0).getSize()).toString();
    }

    public int getColumnsCount(){return rows.get(0).getSize();}


    @Override
    public Iterator<Row> iterator() {
        return rows.iterator();
    }
}
